﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;

namespace teavch.Util.SitemapWriter
{
	public class Sitemap
	{
		public SitemapAttributeIncludeOptions IncludeOptions { get; set; }
		public SitemapUrlSet SitemapUrlSet { get; set; }
		public SupportedSearchEngine TargetSearchEngine { get; set; }
		public string StandardXml { get; set; }
		//public string WebsiteUrl { get; set; }

		public string GetXml( SupportedSearchEngine _searchEngine )
		{
			XmlWriterSettings xws = new XmlWriterSettings();
			xws.Indent = true;
			xws.IndentChars = "\t";
			xws.NewLineChars = System.Environment.NewLine;

			if ( this.IncludeOptions == null )
				this.IncludeOptions = new SitemapAttributeIncludeOptions();
			xws.Encoding = Encoding.UTF8;
			
			StringBuilder sb = new StringBuilder();
			using ( XmlWriter writer = XmlWriter.Create( sb, xws ) )
			{

				writer.WriteStartDocument();
				writer.WriteStartElement( "urlset", "http://www.sitemaps.org/schemas/sitemap/0.9" );

				//writer.WriteAttributeString( "xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9" );

				if ( _searchEngine == SupportedSearchEngine.Google )
				{
					writer.WriteAttributeString( "xmlns", "image", null, "http://www.google.com/schemas/sitemap-image/1.1" );
					writer.WriteAttributeString( "xmlns", "video", null, "http://www.google.com/schemas/sitemap-image/1.1" );
				}

				foreach ( SitemapUrl url in this.SitemapUrlSet.SitemapUrls )
				{
					writer.WriteStartElement( "url" );

					writer.WriteElementString( url.Location.XmlTag, url.Location.Value );

					if ( _searchEngine == SupportedSearchEngine.Google )
					{
						if ( this.IncludeOptions.IncludeImage )
						{
							if ( url.ImageImage != null && url.ImageImage.Location.Count > 0 )
							{
								foreach ( Location location in url.ImageImage.Location )
								{
									writer.WriteStartElement( url.ImageImage.XmlTagPrefix, url.ImageImage.XmlTag, null );
									writer.WriteElementString( location.XmlTagPrefix, location.XmlTag, null, location.Value );
									writer.WriteEndElement();
								}
							}
						}
					}

					if ( this.IncludeOptions.IncludeLastModifiedTag )
						writer.WriteElementString( url.LastModified.XmlTag, url.LastModified.Value );

					if ( this.IncludeOptions.IncludeChangeFrequencyTag )
						writer.WriteElementString( url.ChangeFrequency.XmlTag, url.ChangeFrequency.Value );

					if ( this.IncludeOptions.IncludePriorityTag )
						writer.WriteElementString( url.Priority.XmlTag, url.Priority.Value );

					writer.WriteEndElement();
				}

				writer.WriteEndElement();

			}
			//this.StandardXml = sb.ToString();
			return sb.ToString();
		}

		public Sitemap()
		{
			this.TargetSearchEngine = SupportedSearchEngine.None;
		}

		public Sitemap( SupportedSearchEngine _searchEngine )
			: this()
		{
			this.TargetSearchEngine = _searchEngine;
		}

	}

	public enum SupportedSearchEngine
	{
		Bing,
		Google,
		Yahoo,
		None
	}

	public class SitemapAttributeIncludeOptions
	{

		public bool IncludeLastModifiedTag { get; set; }
		public bool IncludeChangeFrequencyTag { get; set; }
		public bool IncludePriorityTag { get; set; }
		public bool IncludeImage { get; set; }
		public bool IncludeVideo { get; set; }
		public bool IncludeTitle { get; set; }
		public bool IncludeDescription { get; set; }


	
	}

}
